<?php
/**
 * Created by PhpStorm.
 * User: myhyperion
 * Date: 14/12/10
 * Time: 下午2:36
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tencent_fetch extends CI_Controller {
    public $err ;

    function get_weibo_info(){
        $this->load->helper('my_common_helper');
        $url = $this->input->post('url');

        if(empty($url)){
            $rs = array(
                'state' => 1,
                'msg' => 'no url'
            );
            $this->msg($rs);die;
        }
        $content = get_from_curl($url);
        $rs = $this->parse_tencent($content);
        if($rs === false){
            $arr = array(
                'state' => 1,
                'msg' => $this->err
            );
        }else{
            $arr = array(
                'state' => 0,
                'msg' => $rs
            );
        }
        $this->msg($arr);
    }

    function msg($arr){
        header('Content-type: application/json');
        echo json_encode($arr);
    }

    function parse_tencent($content){
        if(empty($content)){
            return false;
        }

        $pattern ='<div class="m_profile e_profile_b clear">(.*?)<\/div>(?=\s*<div id="orginCnt">)';
        preg_match_all('|'.$pattern.'|ims', $content, $matches);

        if(empty($matches[1][0])){
            $this->err = '没有匹配到用户信息';
            return false;
        }

        $userinfo = $matches[1][0];
        $u_pattern = '<div class="avatar"><a href="([^"]*)" title="(.*?)\(@(.*?)\)"[^>]*><img src="(.*?)".*?/></a>.*?<div class="m_profile_info"><p class="summary c_tx5" title="(.*?)">.*?<a class="text_count" href=".*?follower">(\d*)</a>.*?<a class="text_count" href=".*?following">(\d*)</a>';
        preg_match_all('|'.$u_pattern.'|ims', $userinfo, $matches);
        $user = array(
            'id' => $matches[3][0],
            'profile_url' => $matches[1][0], // 主页
            'name' => $matches[2][0],
            'description' => $matches[5][0],
            'header_url' => $matches[4][0], // 头像
            'followers_count' => $matches[6][0],
            'friends_count' => $matches[7][0],
        );
        $v_pattern = '<a title="腾讯个人认证" target="_blank" class="vip">';
        if(preg_match('|'.$v_pattern.'|ims', $userinfo)){
            $user['verified'] = 1; // 认证的
        }else{
            $user['verified'] = 2; // 非认证：线上主库是0, 用户库是2
        }

        $pattern ='<div id="orginCnt">(.*?)<div class="LC" id="LC">';
        preg_match_all('|'.$pattern.'|ims', $content, $matches);

        if(empty($matches[1][0])){
            $this->err = '没有匹配到微博信息';
            return false;
        }
        $contentinfo = $matches[1][0];

        $c_pattern = '<div class="orginCnt e_orginCnt_b clear"  id="(.*?)" rel="(\d*)".*?<div id="msginfo" class="msgCnt">(.*?)</div>.*?<a class="f".*?>(.*?)</a>.*?class="relay" num="(\d*)".*?class="comt" num="(\d*)"';
        preg_match_all('|'.$c_pattern.'|ims', $contentinfo, $matches);
        $weibo = array(
            '_id' => $matches[1][0],
            'addtime' => $matches[2][0],
            'text' => $matches[3][0],
            'comments_count' => $matches[6][0],
            'reposts_count' => $matches[5][0],
            'attitudes_count' => 0, // 点赞数需要登录
            'source' => $matches[4][0]
        );
        $weibo['user'] = $user;
        return $weibo;

    }
}

